package com.D5Power.Objects.Effects
{
	import com.D5Power.basic.GraphicsResource;
	import com.D5Power.display.D5TextField;
	import com.D5Power.scene.BaseScene;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	
	/**
	 * 公告类效果
	 */ 
	public class NoticeEffect extends EffectObject
	{
		protected var delete_fun:Function;
		
		public static var STARTY:uint = 100;
		/**
		 * @param	content	公告内容
		 * @param	config	样式配置器，可包含以下属性：color 字体颜色 borderColor 描边颜色 size 字体颜色
		 */ 
		public function NoticeEffect(scene:BaseScene,content:String,dfun:Function=null,config:Object=null)
		{
			super(scene);
			delete_fun = dfun;
			_stayTime=120;
			_zOrderF = 9999;
			buildBuffer(content,config);
		}
		
		override public function get isOver():Boolean
		{
			return true;
		}
		
		override protected function autoDelete():void{}
		
		protected function buildBuffer(content:String,config:Object):void
		{
			var color:uint = config==null || config.color==null ? 0xFFFFFF : config.color;
			var padding:uint = 1;
			
			var lable:D5TextField = new D5TextField('',color);
			lable.htmlText = content;
			lable.fontBorder = config==null || config.borderColor==null ? 0x000000 : config.borderColor;
			lable.fontSize = config==null || config.size==null ? 14 : config.size;
			if(config!=null && config.width!=null) lable.maxWidth = config.width;
			lable.x = lable.y = padding;
			lable.autoGrow();
			
			var box:Sprite = new Sprite();
			box.addChild(lable);
			if(config!=null && config.bgcolor!=null)
			{
				box.graphics.beginFill(config.bgcolor,(config!=null && config.bgalpha!=null ? .6 : config.bgalpha));
				box.graphics.drawRect(0,0,lable.width+padding*2,lable.height+padding*2);
				box.graphics.endFill();
			}
			
			_buffer = new BitmapData(box.width,box.height,true,0x00000000);
			_buffer.draw(box);
			graphics = new GraphicsResource(new Bitmap(_buffer));
			
			box.removeChild(lable);
			box.graphics.clear();
			box = null;
			lable=null;
		}
		
		override protected function run():void
		{
			if(_stayTime<=0)
			{
				if(_alpha>0)
				{
					_alpha-=0.05;
					_colorPan.alphaMultiplier=0.95;
				}else{
					if(delete_fun!=null) delete_fun(this);
					_scene.removeObject(this);
					return;
				}
			}
			_stayTime--;
		}
	}
}